package com.ruoyi.api.sms;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author LvKinfy
 * Create on 2019/6/21
 * Desc 短信服务
 **/
@Service
public class SMSServiceImpl implements ISMSService {
    //短信服务
    @Override
    public JSONObject smsSend(String templateParam, String phone, String accessKeyId, String secret, String signName, String templateCode) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, secret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.setConnectTimeout(10000);
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", templateParam);
        String result = null;
        try {
            result = Objects.requireNonNull(client.getCommonResponse(request)).getData();

        } catch (Exception e) {
        }
        if (null != result) {
            JSONObject o = JSON.parseObject(result);
//            o.put("smsCode", smsCode);
            if (null != o) {
                o.remove("RequestId");
                o.remove("BizId");
              return o;
            }
        }
      return null;
    }
}
